
************************************************
*                                              *
*  $70 interrupt test by Jedi from Sector One  *
*                                              *
************************************************

        header

        move cs,a
        move a,ds
        bsr installISR

* Comment the following line if you want to kill that nasty demo after
* having pressed a key

        bra.s resid

        xor a,a
        trap #$16
        bsr uninstallISR

        move #$4c00,a
        trap #$21

resid   move #@end,d
        move #$3100,a
        trap #$21

* readRTC : ax = index

readRTC pushf
        intoff
        or.b #$80,a
        out.b a,$70
        bra.s *+2
        in.b $71,a
        push a
        bra.s *+2
        move.b #$d,a
        out.b a,$70
        bra.s *+2
        in.b $71,a
        pop a
        popf

        rts

* setRTC : ax = index ; dl = value

setRTC  pushf
        push a
        intoff

        or.b #$80,a
        out.b a,$70
        bra.s *+2

        move.b d,a

        out.b a,$71
        bra.s *+2
        move.b #$d,a
        out.b a,$70
        bra.s *+2
        in.b $71,a
        pop a
        popf

        rts

installISR
        move #$3570,a
        trap #$21
        cs:move b,old70int
        cs:move es,old70int+2
        move #int70proc,d
        push ds
        move cs,a
        move a,ds
        move #$2570,a
        trap #$21
        pop ds

        move #$b,a
        bsr readRTC
        or.b #$40,a
        move a,d
        move #$b,a
        bsr setRTC
        move #$c,a
        bsr readRTC

        move #$a,a
        bsr readRTC
        and.b #$f0,a
        or.b #8,a
        move a,d
        move #$ah,a
        bsr setRTC

        intoff
        in.b $a1,a
        and.b #$fe,a
        bra.s *+2
        out.b a,$a1
        inton

        rts

uninstallISR
        move #$b,a
        bsr readRTC
        and.b #$bf,a
        move.b a,d
        move #$b,a
        bsr setRTC
        move #$a,a
        bsr readRTC
        and.b #$f0,a
        or.b #6,a
        move a,d
        move #$a,a
        bsr setRTC
        push ds
        move #$2570,a
        cs:lds old70int,d
        trap #$21
        pop ds

        rts

* Here is the routine called periodically

int70proc
        push a
        push d
        push es

        move #$3c8,d
        xor.b a,a
        out.b a,d
        inc d
        out.b a,d
        out.b a,d
jerky   move.b #127,a
        out.b a,d
        cs:inc.b jerky+1

        move #$c,a
        bsr readRTC

        intoff
        move.b #$20,a
        out.b a,$a0
        out.b a,$20
        pop es
        pop d
        pop a
        rti

old70int
        dc.l 0

        segment
        ds.l $400
        stack

        segment
end
